import * as cc from 'cc';

import * as ecs from '@esengine/ecs-framework';
import { LocalTransform } from '../component/LocalTransform';
import { RenderComponent } from '../component/SpriteComponent';

@ecs.ECSSystem('CocosRenderSystem')
export class CocosRenderSystem extends ecs.EntitySystem {
    constructor(public rootNode: cc.Node) {
        // super();
        super(ecs.Matcher.all(LocalTransform, RenderComponent));
        this.rootNode = rootNode;
        console.log('CocosRenderSystem');
    }

    public override process(entities: ecs.Entity[]): void {
        console.log('process over', entities.length);
        this.entities.forEach((entity) => {
            const transform = entity.getComponent(LocalTransform)!;
            const sprite = entity.getComponent(RenderComponent)!;

            if (sprite.cocosNode) {
                sprite.cocosNode.setPosition(
                    transform.position.x,
                    transform.position.y,
                );
                sprite.cocosNode.setScale(
                    transform.scale.x,
                    transform.scale.y,
                );
            }
        });
    }

    // public override update(): void {
    //     const entityQuery = this.scene.querySystem.queryAll(
    //         LocalTransform,
    //         RenderComponent,
    //     );
    //     console.log('update', entityQuery.count);
    //     // this.process(entityQuery.entities);
    // }
}
